package com.codejoys.monitor;

import com.codejoys.monitor.domain.SysConfig;
import com.codejoys.monitor.domain.SysConfigExample;
import com.codejoys.monitor.mapper.SysConfigMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

public class Utils {
    private static final Logger logger = LoggerFactory.getLogger(Utils.class);

    public static void trySleep(long ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // 根据设备sn算设备型号
    // 661F5020AAAB014
    public static String getModelBySn(String sn, int start, int end) {
        if (StringUtils.isEmpty(sn)) {
            logger.warn("无效的设备SN!");
            return "";
        }

        if (start < 0) start = 0;
        if (start >= sn.length()) start = sn.length() - 1;

        if (end <= 0 || end > sn.length()) end = sn.length();

        return sn.substring(start, end);
    }

    public static int[] getModelConfigs(SysConfigMapper mapper) {
        // 查询型号配置
        SysConfigExample example = new SysConfigExample();
        example.createCriteria().andNameEqualTo(Constants.CONFIG_KEY_MODEL_START);
        example.or().andNameEqualTo(Constants.CONFIG_KEY_MODEL_LEN);
        List<SysConfig> sysConfigs = mapper.selectByExample(example);

        int modelStart = Constants.CONFIG_VALUE_MODEL_START;
        int modelLen = Constants.CONFIG_VALUE_MODEL_LEN;
        try {
            if (sysConfigs != null && sysConfigs.size() > 0) {
                for (SysConfig c : sysConfigs) {
                    if (Constants.CONFIG_KEY_MODEL_START.equalsIgnoreCase(c.getName())) {
                        modelStart = Integer.parseInt(c.getValue());
                    }

                    if (Constants.CONFIG_KEY_MODEL_LEN.equalsIgnoreCase(c.getName())) {
                        modelLen = Integer.parseInt(c.getValue());
                    }
                }
            }

            logger.debug("设备型号配置 - modelStart: {}, modelLen: {} ", modelStart, modelLen);
        } catch (Exception e) {
            logger.warn("读取设备型号配置异常！{}", e.getMessage());
        }

        modelStart = modelStart - 1;    // 用户配置从1开始，程序截取字符串从0开始

        return new int[] {modelStart, modelStart + modelLen};
    }

    // 661F50200000000 base=50  power=2
    public static BigDecimal getDevicePower(String sn) {
        if (StringUtils.isEmpty(sn) || sn.matches("^.{4}\\d{3}.+") == false) {
            logger.warn("无效的设备SN!");
            return null;
        }

        try {
            int base = Integer.valueOf(sn.substring(4, 6));
            int power = Integer.valueOf(sn.substring(6, 7));

            return new BigDecimal(base * Math.pow(10, power));
        } catch (Exception e) {
            logger.warn("无效的设备SN!");
            return null;
        }
    }


    public static void main(String[] args) {
        String s1 = "661F502xy";
        String s2 = "661F152d2020329";

        System.out.println(s1.matches("^.{4}\\d{3}.+"));
        System.out.println(s2.matches("^.{4}\\d{3}.+"));

        System.out.println(getDevicePower("661F50200000000").longValue());
        System.out.println(getDevicePower("661F15200000000").longValue());
    }
}
